<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/11/21
 * Time: 15:33
 */

namespace app\admin\controller;


use think\Db;

class User extends Common
{



    protected $beforeActionList = [
        'six'  ,


    ];

    protected function six()
    {
        $this->assign('t',6);

    }

    /*
     * 
     * 用户列表
     * 
     * */
    public function index(){

        $send = input();
        if(isset($send['keyword']) && !empty($send['keyword'])){
            $this->assign('keyword',$send['keyword']);
        }else{
            $this->assign('keyword','');
        }


        return $this->fetch();
    }

    /*
     *
     * 用户列表
     *
     * */
    public function listjson(){

        $user = new \app\admin\model\User();
        $send = input();
        $where = array();
        if(isset($send['keyword']) && !empty($send['keyword'])){
            $where['username'] = ['like','%'.$send['keyword'].'%'];
            $this->assign('keyword',$send['keyword']);
        }else{
            $this->assign('keyword','');
        }
        $page = empty($send['page'])?1:$send['page'];
        $limit = empty($send['limit'])?10:$send['limit'];
        // $users = collection($user->getAllUser($page,$limit))->toArray();
        $users = $user->where($where)->page($page,$limit)->select();
        $count = $user->getCount();
        $resjson = '{ "code": 0, "msg": "","count": '.$count.', "data":'.json_encode($users) .'}';
        echo $resjson;
    }


    /*
     * 
     * 用户列表
     * 
     * */
    public function detail(){

        $send = input();
        $user = new \app\admin\model\User();
        $user = $user->getUserByuid($send['uid']);
//        dump($user);
        $this->assign('user',$user);

        return $this->fetch();
    }

    /*
     * 修改
     *
     * */
    public function modify(){
        $send = input();

        
        Db::startTrans();
        try{
            $usermodel = new \app\index\model\User();
            $usermodel->where('uid',$send['uid'])->update($send);//新闻的表的更新

            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
//            dump($e);
            Db::rollback();
            $this->error('填写的信息错误，请检查！');
            
        }

        $this->success('修改成功');
    }

    /*
     * 删除分类
     *
     * */
    public function delete(){
        $send = input();
//        dump($send);
        Db::startTrans();
        try{
            $usermodel = new \app\index\model\User();
            $usermodel->destroy($send['uid']);//新闻的表的更新

            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
//            dump($e);
            Db::rollback();
            $this->error('删除失败');
            
        }

        $this->success('删除成功');

    }


    /*
     * 增加页面.
     *
     * */
    public function add(){



        return $this->fetch();
    }


    /*
     * 增加操作.
     *
     * */
    public function doadd(){
        $send = input();
//        dump($send);

            $uservalidate = new \app\index\validate\User();
            Db::startTrans();
            try{


                if ($uservalidate->check($send)){
                    $usermodel = new \app\index\model\User();
                    $res = $usermodel->isUpdate(false)->save($send);
                    if(!$res){
                        $this->success('新增成功');
                    }
                }


                // 提交事务
                Db::commit();

            } catch (\Exception $e) {

                Db::rollback();

                $this->error($uservalidate->getError());

            }

            $this->success('新增成功','User/index');



    }
}